package com.wardrobe.soa.backend.practitioners;

import com.google.common.collect.Maps;
import com.wardrobe.domain.booking.BookingBean;
import com.wardrobe.domain.common.BackEndFromBean;
import com.wardrobe.domain.practitioners.PractitionersBean;
import com.wardrobe.domain.practitioners.PractitionersFromBean;
import com.wardrobe.domain.practitioners.PractitionersShowBean;
import com.wardrobe.domain.shop.ShopFromBean;
import com.wardrobe.domain.shop.ShopShowBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018/11/30.
 */
@RestController
@RequestMapping("/backend/practitioners")
public class BackPractitionersController {
    private static final Logger logger = LoggerFactory.getLogger(BackPractitionersController.class);
    @Autowired
    private BackPractitionersService practitionersService;

    @RequestMapping("/practitionserList")
    public Map<String, Object> getPractitionserList(PractitionersFromBean fromBean) {
        Map<String, Object> result = Maps.newHashMap();
        List<PractitionersShowBean> list = practitionersService.practitionserList(fromBean);
        list.stream().forEach(p -> {
            p.setCreateTime(p.getCreateTime().substring(0,p.getCreateTime().length()-2));
        });
        long count = practitionersService.countPractitioners(fromBean);
        int pageCount = (int) Math.ceil(((double) count / fromBean.getPageSize()));
        result.put("list", list);
        result.put("count", count);
        result.put("pageCount", pageCount);
        result.put("pageSize", fromBean.getPageSize());
        return result;
    }

    @RequestMapping("/modifyPractioners")
    public Map<String,Object> modifyPractioners(BackEndFromBean fromBean){
        Map<String,Object> result = Maps.newHashMap();
        try {
            fromBean.setUpdateTime(new Date());
            int status =practitionersService.modifyPractioners(fromBean);
            if(status==1){
                result.put("status",status);
                result.put("msg","修改成功");
            }else{
                result.put("status",2);
                result.put("msg","修改失败");
            }
            return result;
        }catch (Exception e){
            result.put("status",2);
            result.put("msg","修改失败");
            logger.error("modifyPractioners error",e);
        }
        return result;
    }

    @RequestMapping("/savePractitoners")
    public Map<String, Object> savePractitoners(PractitionersBean bean) {
        Map<String, Object> result = Maps.newHashMap();
        try {
            int status = practitionersService.savePractitoners(bean);
            if (status == 1) {
                result.put("status", status);
                result.put("msg", "添加成功");
            } else {
                result.put("status", 2);
                result.put("msg", "添加失败");
            }
            return result;
        } catch (Exception e) {
            result.put("status", 2);
            result.put("msg", "添加失败");
            logger.error("savePractitoners error", e);
        }
        return result;
    }
    @RequestMapping("/deletePractitioners")
    public int deletePractitioners(int id){
        return practitionersService.deletePractitioners(id);
    }
}
